//
//  GSDKUserEmailRegisteredView.m
//  GameSDK
//
//  Created by Johnny on 2017/8/2.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKUserEmailRegisteredView.h"
#import "GSDKCommonMethod.h"
#import "YYKit.h"
#import "GSDKUserConfig.h"
@interface GSDKUserEmailRegisteredView ()<UITextFieldDelegate>
@property (nonatomic,retain) UIImageView * logoImageView;
@property (nonatomic,retain) UIView * contentView;
@property (nonatomic,retain) UIImageView * contentBgView;
@property (nonatomic,retain) UIImageView * contentLineView;
@property (nonatomic,retain) UITextField * unTextField;
@property (nonatomic,retain) UITextField * pwTextField;
@property (nonatomic,retain) UIButton * registerButton;
@property (nonatomic,retain) UIButton * mobileRegButton;
@property (nonatomic,retain) UIButton * oneSecondRegButton;
@property (nonatomic,retain) UILabel * errorLabel;
@property (nonatomic,retain) UIButton * cancelButton;
@property (nonatomic,retain) UIButton * backButton;
@end

@implementation GSDKUserEmailRegisteredView
- (void)dealloc {
    DEBUGMSG(@"GSDKUserEmailRegisteredView dealloc");
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    [self hideError];
}

- (id)init {
    self = [super init];
    if (self != nil) {
        [self setupUI];
    }
    return self;
}

- (void)errorAt:(GSDKUserErrorViewModel*)viewModel {
    if (UserViewWidth < 320) {
        [GSDKMessagePopView showAt:viewModel.errorString andBGColor: [UIColor colorWithHexString:@"#e82929"]];
    } else {
        [self showErrorAtMsg:viewModel.errorString];
    }
}

- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 4;
    
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setTitle:[NSString stringWithFormat:@" %@", [NSString localizedStringForKey:@"GSDK_back_text" value:@"返回"]] forState:UIControlStateNormal];
    [_backButton setTitleColor:[UIColor colorWithHexString:@"#b9b9b9"] forState:UIControlStateNormal];
    _backButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [_backButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/left"] forState:UIControlStateNormal];
    _backButton.frame = CGRectMake(10, 8, 50, 30);
    [self addSubview:_backButton];
    //_backButton.hidden = YES;
    
    _logoImageView = [[UIImageView alloc]init];
    _logoImageView.image = [UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/newLogo"];
    _logoImageView.frame = CGRectMake(UserViewWidth/2 - 119.7/2, 15, 119.7, 35);
    [self addSubview:_logoImageView];
    
    _errorLabel = [[UILabel alloc]init];
    _errorLabel.font = [UIFont systemFontOfSize:10];
    _errorLabel.hidden = YES;
    _errorLabel.alpha = 0.5;
    _errorLabel.textAlignment = NSTextAlignmentCenter;
    _errorLabel.textColor = [UIColor colorWithHexString:@"#ff6b02"];
    _errorLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_errorLabel];
    _errorLabel.layer.borderColor = [UIColor colorWithHexString:@"#ff6b02"].CGColor;
    _errorLabel.layer.borderWidth = 0.5;
    _errorLabel.layer.cornerRadius = 4;
    
    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [_cancelButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/close"] forState:UIControlStateNormal];
    _cancelButton.frame = CGRectMake(UserViewWidth - 40, 10, 30, 30);
    [self addSubview:_cancelButton];
    [self setupContent];
    
//    UILabel * agreementLabel = [[UILabel alloc]init];
//    agreementLabel.frame = CGRectMake(_contentView.frame.origin.x, _contentView.frame.origin.y+_contentView.frame.size.height+5, _contentView.frame.size.width-20, 20);
//    agreementLabel.text = @"     我已阅读并同意用户协议";
//    agreementLabel.textColor = [UIColor colorWithHexString:@"#777777"];
//    agreementLabel.attributedText = [self getAttStringWithGrayString:@"用户协议" andOriginalString:@"     我已阅读并同意用户协议"];
//    agreementLabel.font = [UIFont systemFontOfSize:13];
//    agreementLabel.userInteractionEnabled = true;
//    [self addSubview:agreementLabel];
//    [agreementLabel setHidden:true];
//    
//    _duiImageView = [[UIImageView alloc]init];
//    _duiImageView.frame = CGRectMake(_contentView.frame.origin.x, _contentView.frame.origin.y+_contentView.frame.size.height+8, 15, 15);
//    _duiImageView.image = [UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/dui"];
//    [self addSubview:_duiImageView];
//    [_duiImageView setHidden:true];
//    
//    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTheAgreement:)];
//    [agreementLabel addGestureRecognizer:tap];
    
    _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerButton.frame = CGRectMake(_contentView.frame.origin.x, _contentView.frame.origin.y + _contentView.frame.size.height + 10, _contentView.frame.size.width, 36);
    [_registerButton setBackgroundImage:[[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/btn_normal"] stretchableImageWithLeftCapWidth:10 topCapHeight:10] forState:UIControlStateNormal];
    [_registerButton setBackgroundImage:[[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/btn_selectd"] stretchableImageWithLeftCapWidth:10 topCapHeight:10] forState:UIControlStateSelected];
    _registerButton.titleLabel.font = [UIFont systemFontOfSize:14.5];
    _registerButton.adjustsImageWhenHighlighted = NO;
    [_registerButton setTitle:[NSString localizedStringForKey:@"GSDK_register_text" value:@"注册"] forState:UIControlStateNormal];
    [_registerButton addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_registerButton];
    _registerButton.layer.cornerRadius = 4;
    
    NSString * oneSecondRegText = [NSString stringWithFormat:@" %@", [NSString localizedStringForKey:@"GSDK_quickregistration_text" value:@"一秒注册"]];
    CGSize oneSecondRegSize = [oneSecondRegText sizeWithFont:[UIFont systemFontOfSize:13]
                               constrainedToSize:CGSizeMake(UserViewWidth, 24) lineBreakMode:NSLineBreakByWordWrapping];
    
    _oneSecondRegButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _oneSecondRegButton.titleLabel.font = [UIFont systemFontOfSize:13];
    _oneSecondRegButton.frame = CGRectMake(UserViewWidth - (28 + oneSecondRegSize.width) - _contentView.frame.origin.x, CGRectGetMaxY(_registerButton.frame) + 5, 28 + oneSecondRegSize.width, 24);
    [_oneSecondRegButton setTitleColor:[UIColor colorWithHexString:@"#777777"] forState:UIControlStateNormal];
    [_oneSecondRegButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/registered"] forState:UIControlStateNormal];
    [_oneSecondRegButton addTarget:self action:@selector(oneSecondRegistAction) forControlEvents:UIControlEventTouchUpInside];
    [_oneSecondRegButton setTitle:oneSecondRegText forState:UIControlStateNormal];
    [self addSubview:_oneSecondRegButton];
    
    _mobileRegButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _mobileRegButton.frame = CGRectMake(_oneSecondRegButton.frame.origin.x - _oneSecondRegButton.frame.size.width, CGRectGetMaxY(_registerButton.frame) + 10,80, 24);
    _mobileRegButton.titleLabel.font = [UIFont systemFontOfSize:UserViewWidth <= 315 ? 12 : 13];
    [_mobileRegButton setTitleColor:[UIColor colorWithHexString:@"#777777"] forState:UIControlStateNormal];
    [_mobileRegButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/Mphone"] forState:UIControlStateNormal];
    [_mobileRegButton setTitle:[NSString stringWithFormat:@" %@", [NSString localizedStringForKey:@"GSDK_emailregister_text" value:@"邮箱注册"]] forState:UIControlStateNormal];
    [_mobileRegButton addTarget:self action:@selector(mobileRegistAction) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:_mobileRegButton];
}

- (NSAttributedString *)getAttStringWithGrayString:(NSString *)grayString andOriginalString:(NSString *)originalString {
    NSMutableAttributedString * attString = [[NSMutableAttributedString alloc] initWithString:originalString];
    [attString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#f34755"] range:[originalString rangeOfString:grayString]];
    return attString;
}

- (void)setupContent {
    _contentView = [[UIView alloc]init];
    if (UserViewWidth < 320) {
        _contentView.frame = CGRectMake(10, _logoImageView.frame.size.height + _logoImageView.frame.origin.y + 20, UserViewWidth - 10*2, 88);
    } else {
        _contentView.frame = CGRectMake(30, _logoImageView.frame.size.height + _logoImageView.frame.origin.y + 20, UserViewWidth - 30*2, 88);
    }
    [self addSubview:_contentView];
    
    _contentBgView = [[UIImageView alloc]init];
    _contentBgView.frame = CGRectMake(0, 0, _contentView.frame.size.width, _contentView.frame.size.height);
    [_contentView addSubview:_contentBgView];
    _contentBgView.layer.cornerRadius = 4;
    _contentBgView.layer.borderWidth = 0.5;
    _contentBgView.layer.borderColor = [UIColor colorWithHexString:@"#e4e4e4"].CGColor;
    
    _contentLineView = [[UIImageView alloc]init];
    _contentLineView.backgroundColor = [UIColor colorWithHexString:@"#e4e4e4"];
    _contentLineView.frame = CGRectMake(0, _contentView.frame.size.height/2, _contentView.frame.size.width, 0.5);
    [_contentView addSubview:_contentLineView];
    
    UIImageView * iconImageView = [[UIImageView alloc]init];
    iconImageView.image = [UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/icon_user"];
    iconImageView.frame = CGRectMake(10, _contentView.frame.size.height/2/2 - 22/2, 22, 22);
    [_contentView addSubview:iconImageView];
    iconImageView = [[UIImageView alloc]init];
    iconImageView.image = [UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/icon_pw"];
    iconImageView.frame = CGRectMake(10, _contentView.frame.size.height/2 + _contentView.frame.size.height/2/2 - 22/2, 22, 22);
    [_contentView addSubview:iconImageView];
    
    _unTextField = [[UITextField alloc]init];
    _unTextField.frame = CGRectMake(iconImageView.frame.origin.x * 2 + iconImageView.frame.size.width, 0, _contentView.frame.size.width - ((iconImageView.frame.origin.x * 2 + iconImageView.frame.size.width) + 10), 44);
    _unTextField.placeholder = [NSString localizedStringForKey:@"GSDK_email_placeholaer" value:@"请输入邮箱"];
    _unTextField.returnKeyType = UIReturnKeyNext;
    _unTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _unTextField.delegate = self;
    _unTextField.clearButtonMode = UITextFieldViewModeAlways;
    _unTextField.keyboardType = UIKeyboardTypeEmailAddress;
    _unTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    _unTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _unTextField.font = [UIFont systemFontOfSize:13];
    [_contentView addSubview:_unTextField];
    
    _pwTextField = [[UITextField alloc]init];
    _pwTextField.frame = CGRectMake(iconImageView.frame.origin.x * 2 + iconImageView.frame.size.width, _contentView.frame.size.height/2, _contentView.frame.size.width - ((iconImageView.frame.origin.x * 2 + iconImageView.frame.size.width) + 10), 44);
    _pwTextField.placeholder = [NSString localizedStringForKey:@"GSDK_password_placeholaer" value:@"6-12位字母、数字和下划线"];
    _pwTextField.returnKeyType = UIReturnKeyGo;
    _pwTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _pwTextField.delegate = self;
    _pwTextField.keyboardType = YES;
    _pwTextField.secureTextEntry = NO;
    _pwTextField.clearButtonMode = UITextFieldViewModeAlways;
    _pwTextField.keyboardType = UIKeyboardTypeEmailAddress;
    _pwTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    _pwTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _pwTextField.font = [UIFont systemFontOfSize:13];
    [_contentView addSubview:_pwTextField];
    
    [_unTextField addTarget:self action:@selector(textChangeAction) forControlEvents:UIControlEventEditingChanged];
    [_pwTextField addTarget:self action:@selector(textChangeAction) forControlEvents:UIControlEventEditingChanged];
}

- (void)showErrorAtMsg:(NSString*)msgString {
    _errorLabel.hidden = NO;
    _errorLabel.text = msgString;
    CGSize msgSize = [msgString sizeWithFont:_errorLabel.font
                           constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) lineBreakMode:_errorLabel.lineBreakMode];
    _errorLabel.frame = CGRectMake((_contentView.frame.origin.x + _contentView.frame.size.width) - (msgSize.width + 8), _logoImageView.frame.size.height + _logoImageView.frame.origin.y, msgSize.width + 8, 16);
    
    _contentBgView.layer.borderColor = [UIColor colorWithHexString:@"#ff6b02"].CGColor;
    _contentLineView.backgroundColor = [UIColor colorWithHexString:@"#ff6b02"];
}

- (void)hideError {
    _errorLabel.hidden = YES;
    _contentBgView.layer.borderColor = [UIColor colorWithHexString:@"#e4e4e4"].CGColor;
    _contentLineView.backgroundColor = [UIColor colorWithHexString:@"#e4e4e4"];
}

#pragma mark - Action

- (void)cancelAction {
    [[NSNotificationCenter defaultCenter] postNotificationName:GSDKUserCancelNoticationName object:nil];
}

- (void)registerAction {
    if (_registerBlock && _unTextField.text.length > 0 && _pwTextField.text.length > 0) {
        _registerBlock(_unTextField.text,_pwTextField.text);
    }
}

- (void)tapTheAgreement:(UITapGestureRecognizer *)sender {
    if (_userAgreementBlock) {
        _userAgreementBlock();
    }
}

- (void)mobileRegistAction {
    if (_mobileRegisterBlock) {
        _mobileRegisterBlock();
    }
}

- (void)oneSecondRegistAction {
    if (_oneSecondRegisterBlock) {
        _oneSecondRegisterBlock();
    }
}

- (void)backAction {
    
    if (_backBlock) {
        _backBlock();
    }
}

- (void)textChangeAction {
    if (_unTextField.text.length > 0 && _pwTextField.text.length > 0) {
        _registerButton.selected = YES;
    } else {
        _registerButton.selected = NO;
    }
}

#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == _unTextField) {
        [_pwTextField becomeFirstResponder];
    } else {
        [_pwTextField resignFirstResponder];
        //注册
        [self registerAction];
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    if (textField == _unTextField) {
        _pwTextField.text = @"";
        _unTextField.text = @"";
    } else {
        _pwTextField.text = @"";
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self hideError];
}

@end
